package com.bysc.cmsservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.cmsservice.entity.CmsBanner;
import com.bysc.cmsservice.service.CmsBannerService;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.bysc.commonutils.constant.RedisConstant.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-05-29
 */
@Api(tags = "轮播图管理")
@RestController
@RequestMapping("/cmsservice/cms-banner")
@Slf4j
public class CmsBannerController {
    @Autowired
    private CmsBannerService  cmsBannerService;


    /*
     * clbuId自动判断
     * 新增前端传输clubId
     * */
    @ApiOperation(value = "新增和更新轮播图（仅后台）")
    @PostMapping("addOrUpdateBanner")
    public Results addOrUpdateBanner(
            @ApiParam(name = "CmsBanner", value = "轮播图信息", required = true)
            @RequestBody CmsBanner cmsBanner, HttpServletRequest request) {
        try {
            String TOKEN_clubId= JwtUtils.getClubIdByJwtToken(request);
            if(!StringUtils.isEmpty(TOKEN_clubId)){
                cmsBanner.setClubId(TOKEN_clubId);
            }
            cmsBannerService.saveOrUpdate(cmsBanner);
            return Results.ok().message(MessageConstant.EDIT_BANNER_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.EDIT_BANNER_FAIL);
        }
    }

    /*
    *
     * 新增根据clubId查询 所有 轮播图
     * */
    @ApiOperation(value = "根据clubId查询对应轮播图（仅单个社团的页面）")
    @GetMapping("/getBannerList/{clubId}")
    public Results getBannerList(@PathVariable String clubId) {
        // 调用service查询全部轮播图
        try {
            //查询根据clubId的所有轮播图
            LambdaQueryWrapper<CmsBanner> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CmsBanner::getClubId, clubId);
            List<CmsBanner> banners = cmsBannerService.list(wrapper);
            return Results.ok().message(MessageConstant.QUERY_BANNER_SUCCESS).data("bannerList", banners);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_BANNER_FAIL);
        }
    }

    @ApiOperation(value = "根据id查询轮播图")
    @GetMapping("/getBannerById/{id}")
    public Results getBannerById(
            @ApiParam(name = "id", value = "轮播图ID", required = true) @PathVariable("id") String id) {
        try {
            CmsBanner getBanner = cmsBannerService.getById(id);
            return Results.ok().message(MessageConstant.QUERY_BANNER_SUCCESS).data("banner", getBanner);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_BANNER_FAIL);
        }
    }

    /*
     * 分页展示轮播图
     * （仅后台）
     * */
    @ApiOperation(value = "分页展示轮播图（仅后台）")
    @PostMapping("/getBannerList/{page}/{limit}")
    public Results getBannerList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "bannerQuery", value = "查询对象", required = false)
            @RequestBody CmsBanner cmsBanner, HttpServletRequest request
    ) {
        try {
            String TOKEN_clubId= JwtUtils.getClubIdByJwtToken(request);
            LambdaQueryWrapper<CmsBanner> wrapper = new LambdaQueryWrapper<>();
            // 根据 CmsBanner 对象的属性构建查询条件
            if (cmsBanner != null) {
                if (!StringUtils.isEmpty(cmsBanner.getId())) {
                    wrapper.eq(CmsBanner::getId, cmsBanner.getId());
                }
                //新增根据clubId查询
                if (!StringUtils.isEmpty(cmsBanner.getClubId())) {
                    wrapper.eq(CmsBanner::getClubId, cmsBanner.getClubId());
                }
                else{
                    if(!StringUtils.isEmpty(TOKEN_clubId)){
                        wrapper.eq(CmsBanner::getClubId, TOKEN_clubId);
                    }

                }
            }
            Page<CmsBanner> pageParam = new Page<>(page, limit);
            IPage<CmsBanner> bannerPage = cmsBannerService.page(pageParam, wrapper);
            return Results.ok()
                    .message(MessageConstant.QUERY_BANNER_SUCCESS)
                    .data("bannerPage", bannerPage);

        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_BANNER_FAIL);
        }
    }


    /*
    * 获取所有轮播图的
    * */
    @ApiOperation(value = "获取所有轮播图的")
    @PostMapping("/getAllBanner")
    public Results getAllBanner() {
        try {
            List<CmsBanner> banners = cmsBannerService.list(new LambdaQueryWrapper<>());
            return Results.ok().message(MessageConstant.QUERY_BANNER_SUCCESS).data("bannerList", banners);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_BANNER_FAIL);
        }
    }


    @ApiOperation(value = "根据id删除轮播图")
    @DeleteMapping("/deleteBannerById/{id}")
    public Results deleteBannerById(
            @ApiParam(name = "id", value = "轮播图ID", required = true) @PathVariable("id") String id
    ) {
        try {
            cmsBannerService.removeById(id);
            return Results.ok().message(MessageConstant.DELETE_BANNER_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.DELETE_BANNER_FAIL);
        }
    }
}

